package com.libl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.libl.mapper.DtsAdminMapper;
import com.libl.mapper.DtsBrandMapper;
import com.libl.pojo.DtsAdmin;
import com.libl.pojo.DtsBrand;
import com.libl.service.DtsAdminService;
import com.libl.util.bcrypt.BCryptPasswordEncoder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DtsAdminServiceImpl implements DtsAdminService {
    @Autowired
    DtsAdminMapper adminMapper;

    @Override
    public DtsAdmin findByUsername(String username) {
        //通过mybatis-plus根据用户名获取admin对象
        LambdaQueryWrapper<DtsAdmin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsAdmin::getUsername, username);

        DtsAdmin admin = adminMapper.selectOne(wrapper);

        return admin;
    }

    //使用mybatis-plus无需使用分页插件，直接创建Page对象即可
    @Override
    public Map<String, Object> queryPage(Integer page, Integer limit, String username, String sort, String order) {
        //创建ipage对象
        IPage<DtsAdmin> ipage = new Page<>(page,limit);
        // 封装查询条件
        QueryWrapper<DtsAdmin> wrapper = new QueryWrapper<>();
        //如果username不为null则添加条件
        if(! StringUtils.isEmpty(username)) {
            wrapper.like(username , username);
        }
        //添加排序条件
        if(!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order) ) {
            if("desc".equals(order)) {
                wrapper.orderByDesc(sort);
            }else {
                wrapper.orderByAsc(sort);
            }
        }
        adminMapper.selectPage(ipage, wrapper);

        long total = ipage.getTotal();
        List<DtsAdmin> items = ipage.getRecords();

        Map<String,Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", items);
        return data;
    }
    BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
    @Override
    public DtsAdmin insert(DtsAdmin admin) {
        //对admin中的数据进行维护
        //对密码进行加密
        String encode = passwordEncoder.encode(admin.getPassword());
        admin.setPassword(encode);

        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        admin.setDeleted(false);

        //新增操作
        adminMapper.insert(admin);
        return admin;
    }

    @Override
    public DtsAdmin update(DtsAdmin admin) {
        //在更新前要对数据进行调整，因为前端密码显示的也是加密后的
        //根据id查询数据库中的admin对象
        DtsAdmin adminDb = adminMapper.selectById(admin.getId());
        //对比前后端密码是否一致，一致则说明不用调整,不一致则说明此次需要设置新的密码
        if(! admin.getPassword().equals(adminDb.getPassword())) {
            admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        }

        admin.setUpdateTime(new Date());
        adminMapper.updateById(admin);
        return admin;
    }

    @Autowired
    DtsBrandMapper brandMapper;

    @Override
    public void delete(DtsAdmin admin) {
        //逻辑删除
        /*admin.setDeleted(true);
        adminMapper.updateById(admin);*/

        //真正删除，注意：删除前要先判断是否被其他对象所关联！
        //判断是否被品牌所关联
        LambdaQueryWrapper<DtsBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsBrand::getAdminId, admin.getId());
        Integer count = brandMapper.selectCount(wrapper);
        if(count > 0) {
            throw new RuntimeException("管理员已被品牌关联，无法删除");
        }

        adminMapper.deleteById(admin.getId());
    }

    @Override
    public List<DtsAdmin> findAll() {
        return adminMapper.selectList(null);
    }

}
